package b1.mobile.blackberry.dao;

import java.util.Vector;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import b1.mobile.blackberry.controller.AgentController;
import b1.mobile.blackberry.entity.KPIEntity;
import b1.mobile.blackberry.io.http.ResponseParserHelper;
import b1.mobile.blackberry.io.http.Serviceworker;
import b1.mobile.blackberry.io.http.SoapContentProvider;

public class KPIDAO extends CommonDAO {
	public static String OP_LIST ="GetKPI";
	
	
	public KPIDAO(DataAccessDelegate delegate){
		super(delegate);
	}
	
	public void onGetResult(Serviceworker worker) {
		if(worker.responseCode == 200){
			parseList(worker);
		}
	}
	
	public void list(){
		SoapContentProvider soap = new SoapContentProvider(OP_LIST, new String[]{}, new String[]{});
		try{
			Serviceworker worker = new Serviceworker(this, AgentController.getServer(), soap);
			new Thread(worker).start();//UiApplication.getUiApplication().invokeLater(worker);
		} catch (Exception ex){
		
		}
	}
	
	protected void parseList(Serviceworker worker){
		ResponseParserHelper help = new ResponseParserHelper(worker.responseMessage);
		JSONArray array = (JSONArray)help.parse("GetKPIResult");
		
		Vector items = new Vector();
		for(int i = 0; i < array.length(); i++){
			try {
				JSONObject json = array.getJSONObject(i);
				KPIEntity ety = new KPIEntity(json);
				items.addElement(ety);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		_delegate.onDataReady(worker, items);
	}
}
